#include<iostream>
using namespace std;

int main()
{
    int p,e,i,d;
	int p1=23,p2=28,p3=33;
	int pp,ee,ii,dd;
	int max=21252;
	int k=1;
	while(cin>>p>>e>>i>>d&&p!=-1)
	{
		pp=p%p1;ee=e%p2;ii=i%p3;dd=d+1;
		while(1)
		{		
			while(pp<dd)
				pp+=p1;
			while(ee<dd)
				ee+=p2;
			while(ii<dd)
				ii+=p3;
			if(pp-d>max||ee-d>max||ii-d>max)
			{
				cout<<"Case "<<k<<": the next triple peak occurs in "<<max-d<<" days."<<endl;
				k++;
				break;
			}
			if(pp==ee&&ii==ee)
			{
				cout<<"Case "<<k<<": the next triple peak occurs in "<<pp-d<<" days."<<endl;	
				k++;
				break;
			}
			
			dd=pp>ee?pp:ee;
			dd=dd>ii?dd:ii;
		}
	}
	return 0;
}